﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class ProductsVariousProvider : ProductsBaseProvider
    {
        private ImageProvider _iprov;

        internal ProductsVariousProvider(ObSession obSession, int productId)
            : base(obSession, productId)
        {
            _iprov = new ImageProvider();
        }

        internal ProductsVariousProvider() { }

        internal int InsertUpdateProductVarious(Product_ProductVarious product, ObSession obSession)
        {
            var res = 0;
            if ((ProfileType)obSession.IdProfile == ProfileType.Admin)
            {
                var query = _db.Product_ProductVarious.Where(w => w.Id == product.Id);
                if (query.Count() > 0) // Update
                {
                    query.First().DescripcionCorta = product.DescripcionCorta;
                    query.First().DescripcionLarga = product.DescripcionLarga;
                    query.First().Detalles = product.Detalles;
                    query.First().IsDestacado = product.IsDestacado;
                    query.First().IsOferta = product.IsOferta;
                    query.First().Nombre = product.Nombre;
                    query.First().ShowAtHome = product.ShowAtHome;
                }
                else
                {
                    Int32 productId = 0;
                    productId = CompiledQuerys.GetProductId(_db, productId);
                    product.Id = productId;
                    _db.Product_ProductVarious.InsertOnSubmit(product);
                }
                _db.SubmitChanges();
                res = product.Id;
            }
            return res;
        }
        internal void DeleteProductVarious()
        {
            this.DeleteStep1();
            base.DeleteStep2();
            base.DeleteStep3();
            base.DeleteStep4();
            base.DeleteStep5();
            base.DeleteStep6();
        }
        internal override void DeleteStep1()
        {
            var query = _db.Product_ProductVarious.Where(w => w.Id == this._productId);
            if (query.Count() > 0)
            {
                _db.Product_ProductVarious.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }
        }
        internal List<object> GetAllProductsVarious()
        {
            var res = new List<object>();
            var query = from b in _db.spGetAllProductsVarious()
                        select new
                        {
                            Id = b.Id,
                            Name = b.Name,
                            ShortDescription = b.ShortDescription,
                            LowerPrice = b.LowerPrice,
                            DescripcionLarga = b.DescripcionLarga,
                            PictureUrl = b.PictureUrl
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    Name = item.Name,
                    ShortDescription = item.ShortDescription,
                    LowerPrice = item.LowerPrice,
                    DescripcionLarga = item.DescripcionLarga,
                    PictureUrl = item.PictureUrl
                });
            }
            return res;
        }
        internal List<Product_ProductVarious> GetAllProductsVariousFull()
        {
            var query = _db.Product_ProductVarious.Select(w => w);
            return query.Count() > 0 ? query.ToList() : new List<Product_ProductVarious>();
        }
        internal Product_ProductVarious GetProductVariousId(int id)
        {
            var res = new Product_ProductVarious();
            var query = _db.Product_ProductVarious.Where(w => w.Id == id);
            res = query.FirstOrDefault<Product_ProductVarious>();
            return res;
        }
        internal void InsertUpdateProductVariousDetail(Product_ProductDetail productDetail)
        {
            DeleteProductVariousDetail(productDetail.Product_ProductId);
            var query = _db.Product_ProductDetail.Where(w => w.Id == productDetail.Id);
            if (query.Count() > 0)
            {
                query.First().CodigoProducto = productDetail.CodigoProducto;
                query.First().Descripcion = productDetail.Descripcion;
                query.First().IsAcompanamiento = productDetail.IsAcompanamiento;
                query.First().Precio = productDetail.Precio;
                query.First().PrecioPromocion = productDetail.PrecioPromocion;
                query.First().Stock = productDetail.Stock;
            }
            else
            {
                _db.Product_ProductDetail.InsertOnSubmit(productDetail);
            }
            _db.SubmitChanges();
        }
        internal void DeleteProductVariousDetail(int productid)
        {
            var query = _db.Product_ProductDetail.Where(w => w.Product_ProductId == productid);
            if (query.Count() > 0) { _db.Product_ProductDetail.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
    }
}
